<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>出入库财务汇总</title>
</head>

<body>
  <div id="app" v-loading.fullscreen.lock="loading">
    <div class="inout-warehouse-balance" v-if="oldSearch!==null">
      <search-btn-view immediate btn-text="出入库财务查询" @before-open="resetSearch" @search="searchData">
        <template slot="append">
          <el-button type="primary" size="medium" @click="print" v-if="printData.length>0">打印</el-button>
          <el-button size="medium" type="primary" @click="exportExcel">导出excel</el-button>
          <span style="margin-left:20px;">入库总额:￥{{inPrice}}</span>
          <span style="margin-left:10px;">出库总额:￥{{outPrice}}</span>
        </template>
        <template slot="right">
          <table-col-show-select v-if="tableHideColumnKey!==null" :table-name="tableName" :current-column-name="currentColumnName"
            :hide-column.sync="tableHideColumnKey">
          </table-col-show-select>
        </template>
        <div slot="search-content">
          <search-form :search="search"></search-form>
        </div>
      </search-btn-view>

      <div v-if="recordInStatList.length>0" style="margin-bottom:5px" class="warehouse-balance-stat">
        <span style="margin-right:5px">入库</span>
        <template v-for="item in recordInStatList">
          <span :key="'materialName_'+item.materialName">{{item.materialName}}:{{item.count}}</span>
        </template>
      </div>

      <div v-if="recordOutStatList.length>0" style="margin-bottom:5px" class="warehouse-balance-stat">
        <span style="margin-right:5px">入库</span>
        <template v-for="item in recordOutStatList">
          <span :key="'materialName_'+item.materialName">{{item.materialName}}:{{item.count}}</span>
        </template>
      </div>

      <div class="warehouse-balance-table-div">
        <el-table fit stripe border ref="inoutWarehouseOrder" :data="balanceList" style="width:100%;" height="parent"
          @selection-change="selectPrintData" :hide-column-keys="getTableHideColumns()">
          <el-table-column fixed :resizable="false" type="selection" label="打印" width="70px" key="printData" column-key="printData">
          </el-table-column>
          <el-table-column :resizable="false" prop="typeText" key="typeText" label="类型" width="60px" align="center"> </el-table-column>
          <el-table-column :resizable="false" column-key="inDay" key="inDay" label="入库日期" width="100px" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.inDay!==''">{{moment(scope.row.inDay).format('YYYY-MM-DD')}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="companyOrderId" key="companyOrderId" label="订单编号" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="customerNo" key="customerNo" label="客户型号" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="customerName" key="customerName" label="客户名称" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="factoryNo" key="factoryNo" label="工厂型号" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="providerName" key="providerName" label="供应商" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="materialName" key="materialName" label="物料名称" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="model" key="model" label="规格" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="color" key="color" label="编号" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="colorDescription" key="colorDescription" label="描述" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="defaultWarehouse" key="defaultWarehouse" label="默认仓位" min-width="100px" align="center"></el-table-column>
          <el-table-column prop="materialDeptName" key="materialDeptName" label="物料部门" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="position" key="position" label="仓位" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="balancePrice" key="balancePrice" label="单价" min-width="100px" header-align="center"></el-table-column>
          <el-table-column prop="inOutUnit" key="inOutUnit" label="单位" min-width="100px" align="center"></el-table-column>
          <el-table-column prop="count" key="count" label="数量" min-width="100px" align="center"></el-table-column>
          <el-table-column sortable column-key="totalMoney" key="totalMoney" label="金额" min-width="140px" align="center">
            <template slot-scope="scope">
              <span>{{getTotalMoney(scope.row)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="isTax" key="isTax" label="是否含税" min-width="100px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg('common.isTax.'+scope.row.isTax)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="remark" key="remark" label="备注" min-width="100px"> </el-table-column>
          <el-table-column prop="createUser" key="createUser" label="制单人" min-width="100px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="createTime" key="createTime" label="制单时间" width="85px" align="center"></el-table-column>
          <el-table-column column-key="approvalUser" key="approvalUser" label="审核人" min-width="100px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" prop="approvalTime" key="approvalTime" label="审核时间" width="85px" align="center"></el-table-column>
        </el-table>
      </div>

      <div v-if="pagination.totalCount!==0" style="text-align:right;margin-top:5px;">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
          @current-change="pageChange"></el-pagination>
      </div>
    </div>

    <el-dialog fullscreen class="dialog-fullscreen" :title="printTempl===null&&templateType==='in'?'入库单':'出库给客人打印'" :visible.sync="openPrintDialog">
      <div style="height:100%;overflow:auto">
        <template v-if="openPrintDialog">
          <template v-if="printTempl===null">
            <print-warehouse v-if="templateType==='out'" :records="printData"></print-warehouse>
            <print-in-warehouse v-else :records="printData"></print-in-warehouse>
          </template>
          <print-warehouse-template v-else :print-data="printData" :template="printTempl"></print-warehouse-template>
        </template>
      </div>
    </el-dialog>
  </div>
</body>
<script src="https://upload.shinehao.com.cn/20181222140744qiniu.min.js"></script>
<script src="https://upload.shinehao.com.cn/20190416085621dom-to-image.min.js"></script>
<script src="../../myJs/importFile.js"></script>
<script>
  importFile([
    "/vueSystem/components/css/sysCommonSearch.css",
    "/vueSystem/components/css/searchBtnView.css",
    "/vueSystem/components/css/showTextarea.css",
    "/vueSystem/components/css/vueEditor.css",
    "/vueSystem/components/css/sysPrintTemplateOrder.css",
    "/vueSystem/mixins/printOrderConfigButton.css",
    "/warehouse/mixins/warehouseInoutPrint.css"
  ], [
    "/vueSystem/i18n/index.js",
    "/vueSystem/components/sysCommonSearch.js",
    "/vueSystem/components/searchBtnView.js",
    "/vueSystem/components/showTextarea.js",
    "/vueSystem/components/config/templateColumnConfig.js",
    "/vueSystem/components/sysPrintTemplateOrder.js",
    "/vueSystem/models/baseModel.js",
    "/vueSystem/models/templateConfig.js",
    "/vueSystem/js/commonTemplateUtil.js",
    "/vueSystem/mixins/printOrderMixin.js",
    "/vueSystem/mixins/printCommonOrderMixin.js",
    "/vueSystem/mixins/datePickerOptionsMixin.js",
    "/warehouse/mixins/printWarehouseOrderTemplateMixin.js",
    "/warehouse/mixins/printWarehouseInoutTemplMixin.js",
    "/warehouse/warehouseInOutDetail/printWarehouseTemplate.js",
    "/warehouse/warehouseInOutDetail/printWarehouse.js",
    "/warehouse/warehouseInOutDetail/printInWarehouse.js"
  ]);
  importTableColShowConfigFiles();

</script>
<script src="./searchForm.js"></script>

<script type="text/javascript">
  var app = new Vue({
    el: '#app',

    mixins: [
      printOrderMixin('inoutWarehouseOrder'),
      printWarehouseOrderTemplateMixin,
      tableColShowMixin('inoutWarehouseOrder')
    ],

    data: function () {
      var search = {
        materialName: '',
        model: '',
        color: '',
        providerName: '',
        materialDept: '',
        orderId: '',
        companyOrderId: '',
        factoryNo: '',
        customerNo: '',
        customerName: '',
        createUser: '',
        type: '',
        queryType: '',
        startDate: null,
        endDate: null,
        timeType: 'create',
        warehouseType: '',
        inStartDay: null,
        inEndDay: null,
        startPrice: undefined,
        endPrice: undefined
      };
      return {
        loading: false,
        search: search,
        oldSearch: null,
        balanceList: [],
        inPrice: '',
        outPrice: '',
        printData: [],
        user: null,
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        },
        recordInStatList: [],
        recordOutStatList: []
      };
    },

    created: function () {
      this.loading = true;
      var self = this;
      this.setUser().then(function () {
        self.loading = false;
      }).catch(function () {
        self.loading = false;
      });
    },

    methods: {
      setUser: function () {
        var self = this;
        return SysUtil.getUser().then(function (user) {
          var fixedCreateUser = _.get(user, 'userPagePermission.fixedWarehouseCreateUser');

          var search = self.search;
          search.fixedCreateUser = fixedCreateUser;
          if (fixedCreateUser) {
            search.createUser = user.userId;
            self.search = _.cloneDeep(search);
          }

          self.oldSearch = _.cloneDeep(search);
          self.user = user;
          return user;
        });
      },

      setBalanceList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize,
          status: 1,
          isFinance: 1,
        };
        this.setPostData(postData);

        this.loading = true;
        var self = this;
        ajaxRequest("warehouse/getWarehouseInOutDetaliListFromFinance", postData, true).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var balanceList = res.listData.returnData;
          for (var i = 0; i < balanceList.length; i++) {
            var balance = balanceList[i];
            if ([1, 3, 4, 7, 8, 11].indexOf(balance.type) !== -1) {
              balance.typeText = "入库";
              balance.count = balance.sumMoneyCount;
            } else {
              balance.typeText = "出库";
              balance.count = balance.count;
            }
            balance.balancePrice = balance.inCountPrice;
            SysUtil.tableSetDeptData(balance);

            var inDay = balance.inDay;
            if (SysUtil.isEmpty(inDay)) {
              balance.inDay = '';
            } else {
              balance.inDay = moment(inDay).valueOf();
            }
          }
          self.balanceList = balanceList;

          var inPrice = res.inPrice;
          if (SysUtil.isEmpty(inPrice)) {
            inPrice = 0;
          } else {
            inPrice = _.round(inPrice, 2);
          }
          self.inPrice = inPrice;

          var outPrice = res.outPrice;
          if (SysUtil.isEmpty(outPrice)) {
            outPrice = 0;
          } else {
            outPrice = _.round(outPrice, 2);
          }
          self.outPrice = outPrice;
          self.pagination.totalCount = res.listData.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      setPostData(postData) {
        var search = this.search;
        var fields = ['materialName', 'model', 'color', 'providerName', 'companyOrderId', 'orderId', 'factoryNo',
          'customerNo', 'customerName', 'createUser', 'type', 'queryType', 'warehouseType', 'startPrice', 'endPrice'
        ];
        SysUtil.setPostData(postData, search, fields);
        var materialDept = search.materialDept;
        if (materialDept !== '') {
          postData.materialDeptId = materialDept.materialDeptId;
        }

        if (search.startDate !== null) {
          postData.startDate = moment(search.startDate).format('YYYY-MM-DD');
        }
        if (search.endDate !== null) {
          postData.endDate = moment(search.endDate).format('YYYY-MM-DD');
        }
        if (search.inStartDay !== null) {
          postData.inStartDay = moment(search.inStartDay).format('YYYY-MM-DD');
        }
        if (search.inEndDay !== null) {
          postData.inEndDay = moment(search.inEndDay).format('YYYY-MM-DD');
        }

        var timeType = search.timeType;
        if (timeType !== 'create') {
          postData.timeType = 'balance';
        }
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.setBalanceList(true);
      },

      pageChange: function () {
        this.setBalanceList();
      },

      selectPrintData: function (selection) {
        this.printData = selection;

        this.calStatData();
      },

      calStatData: function () {
        var warehouseList = this.printData;

        var recordInStatObj = {},
          recordOutStatObj = {};
        for (var i = 0; i < warehouseList.length; i++) {
          var warehouse = warehouseList[i];
          if (warehouse.typeText === '入库') {
            this.calWarehouseStat(recordInStatObj, warehouse);
          } else {
            this.calWarehouseStat(recordOutStatObj, warehouse);
          }
        }
        this.recordInStatList = _.values(recordInStatObj);
        this.recordOutStatList = _.values(recordOutStatObj);
      },

      calWarehouseStat: function (statObj, warehouse) {
        var materialName = warehouse.materialName;
        var obj = statObj[materialName];
        var count = warehouse.count;
        if (obj === undefined) {
          obj = {
            materialName: materialName,
            count: count
          };
          statObj[materialName] = obj;
        } else {
          obj.count += count;
        }
      },

      getTotalMoney: function (balance) {
        var totalMoney = balance.inOutMoney;
        if (!SysUtil.isEmpty(totalMoney) && totalMoney !== 0) {
          return totalMoney;
        }

        var balancePrice = balance.balancePrice,
          count = balance.count;
        if (SysUtil.isEmpty(balancePrice) || SysUtil.isEmpty(count)) {
          return '';
        }

        return _.round(balancePrice * count, globalConfig.pricePrecision);
      },

      exportExcel: function () {
        var postData = {
          status: 1,
          isFinance: 1,
        };
        this.setPostData(postData);
        var formParam = SysUtil.formatFormParam(postData);
        var basePath = SysUtil.getBasePath();
        var path = `${basePath}/warehouse/exportExcelFileByWarehouse?${formParam}`;
        window.open(path);
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      SearchForm: SearchForm,
      PrintWarehouse: PrintWarehouse,
      PrintInWarehouse: PrintInWarehouse,
      PrintWarehouseTemplate: PrintWarehouseTemplate
    }
  });

</script>
<style>
  .inout-warehouse-balance {
    height: calc(100% - 15px);
    padding: 10px 20px 5px;
    display: flex;
    flex-direction: column;
  }

  .inout-warehouse-balance .warehouse-balance-table-div {
    flex: 1;
    overflow: auto;
  }

  .inout-warehouse-balance .warehouse-balance-stat>span {
    margin-right: 10px;
  }

  .inout-warehouse-balance .warehouse-balance-stat>span:last-child {
    margin-right: 0;
  }

</style>

</html>
